<?php
require '../../../loader.php';
require 'common.php';

if (isset($_POST['type'])) {
    $type = $_POST['type'];
}else{
    error('参数错误');
}

if ($type != 'database' &&  $type!='cache'){
    error('参数错误');
}

$data = $_POST['data'];
$driver = strtolower($_POST['driver']);

$className =  '\\bibi\\'.$type.'\\'.ucfirst($driver);

if (class_exists($className)){
    
    try{
        $obj = new $className($data);
    }catch (Exception $e){
        error($e->getMessage());
    }
}else{
    error('驱动类不存在');
}

$configPath = ROOT.DS.CONFIG.DS.$type.'.php';

if (!file_exists($configPath)) {
    $config = [];
    $config['default'] = $driver;
    $data['driver'] =  strtolower($driver);
    $config[$driver] = $data;
    $config = "<?php\nreturn ".var_export($config,true).";\n";
    file_put_contents($configPath,$config);
    success('保存成功');
}else{
    error('配置文件已存在，如果要修改，请先删除配置文件 '."\n\n".$configPath);
}
